<?php
/**
 * 유틸리티 - Captcha 이미지
 *
 * @package    util
 * @subpackage captcha
 *
 * @version 1.0
 * @author    jylee3@simplexi.com
 */
class utilCaptchaImage
{
    protected $iWidth;
    protected $iHeight;
    protected $sCode;
    protected $rImage;
    protected $sImageType;

    /**
     * Captcha 이미지 객체 생성자
     *
     * @param    Integer    $iWidth
     * @param    Integer    $iHeight
     * @param    String    $sImageType
     * @param    String    $sCode
     * @param    GD Resource    $rImage
     */
    public function __construct($iWidth, $iHeight, $sImageType, $sCode, $rImage)
    {
        $this->iWidth = $iWidth;
        $this->iHeight = $iHeight;
        $this->sImageType = strtoupper($sImageType);
        $this->sCode = $sCode;
        $this->rImage = $rImage;
    }

    /**
     * Captcha 이미지 넓이 반환
     *
     * <code>
     * $utilCaptchaImage->getWidth();
     * </code>
     *
     * @return    Integer
     */
    public function getWidth()
    {
        return $this->iWidth;
    }

    /**
     * Captcha 이미지 높이 반환
     *
     * <code>
     * $utilCaptchaImage->getHeight();
     * </code>
     *
     * @return    Integer
     */
    public function getHeight()
    {
        return $this->iHeight;
    }

    /**
     * Captcha 코드 반환
     *
     * <code>
     * $utilCaptchaImage->getCode();
     * </code>
     *
     * @return    String
     */
    public function getCode()
    {
        return $this->sCode;
    }

    /**
     * Captcha 이미지 리소스(GD) 반환
     *
     * <code>
     * $utilCaptchaImage->getImage();
     * </code>
     *
     * @return    GD Resource
     */
    public function getImage()
    {
        return $this->rImage;
    }

    /**
     * Captcha 이미지 종류 반환
     *
     * <code>
     * $utilCaptchaImage->getImageType();
     * </code>
     *
     * @return    String
     */
    public function getImageType()
    {
        return $this->sImageType;
    }

    /**
     * Captcha 이미지 출력
     *
     * ※ 이미지 출력 후 이미지 리소스는 파괴됩니다.
     *
     * <code>
     * $utilCaptchaImage->outputCaptchaImage();
     * </code>
     *
     * @param    Boolean    $bOutputMode
     *
     * @return    Boolean
     */
    public function outputCaptchaImage($bOutputMode=true)
    {
        $sMethodName = __FUNCTION__.'_'.$this->sImageType;

        if(!utilSplClass::hasMethod($this, $sMethodName))    return false;

        call_user_func(array($this, $sMethodName), $bOutputMode);

        return true;
    }

    /**
     * Captcha 이미지 출력 - PNG
     *
     * @param    Boolean    $bOutputMode
     *
     * @return    void
     */
    protected function outputCaptchaImage_PNG($bOutputMode=true)
    {
        header('Content-Type: image/png');

        if ($bOutputMode) imagepng($this->rImage);

        imagedestroy($this->rImage);
    }

    /**
     * OCaptcha 이미지 출력 - JPEG
     *
     * @param    Boolean    $bOutputMode
     *
     * @return    void
     */
    protected function outputCaptchaImage_JPEG($bOutputMode=true)
    {
        header('Content-Type: image/jpeg');

        if ($bOutputMode)    imagejpeg($this->rImage);

        imagedestroy($this->rImage);
    }
}
